<?php
declare (strict_types = 1);

namespace app\admin\event;

use app\admin\model\Columnmodel;
use app\admin\model\Rolemodel;
use think\Facade\Cache;

class ChangeRoleColumn{

    protected $handler;
    protected $roleModel;
    protected $columnModel;

    public function __construct(Columnmodel $columnModel, Rolemodel $roleModel){
        $this->roleModel = $roleModel;
        $this->columnModel = $columnModel;
        $this->handler = Cache::store('redis')->handler();
    }

    public function setColumnData(){
//        if($rid){
//            //更新指定角色栏目权限
//            $this->handler->delete('columnRoleData', $rid);
//            $this->handler->delete('actionPowerData', 'actionPower_'.$rid);
//            $roleRes = $this->roleModel->findAllInfo('id, role_column', [['is_delete', '=', 1], ['id', '=', $rid]]);
//        }else{
//            //更新全部栏目权限
//            $roleRes = $this->roleModel->findAllInfo('id, role_column', [['is_delete', '=', 1]]);
        $this->handler->delete('columnRoleData');
        $this->handler->delete('actionPowerData');
//        }

        $roleRes = $this->roleModel->findAllInfo('id, role_column', [['is_delete', '=', 1]]);
        $columnRes = $this->columnModel->findAllInfo('id, name, level, father_id, action_id, icon, url, status, model', [['status', '=', 1]]);
        $this->handler->hset('columnRoleData', 'column_0', json_encode($columnRes));

        $fatherData = $idsArr = $data = array();
//        $actionArr = [
//            '1'=>'add',
//            '2'=>'edit',
//            '3'=>'del',
//            //'4'=>'审核',
//        ];
        $actionArr = json_decode($this->handler->hget('action', 'action_-1'), true);
        foreach($columnRes as $val){
            $fatherData[$val['id']] = $val;
        }

        foreach($roleRes as $roleVal){

            if($roleVal['role_column']){
                $roleFatherData = array();
                $idsArr = explode(',', $roleVal['role_column']);
                foreach($idsArr as $idsVal){
                    $roleFatherData[$idsVal] = $fatherData[$idsVal];
                    //获取角色对应增删改权限
                    if(strpos($idsVal,'_') !== false){
                        $columnRolePower = explode('_', $idsVal);
                        $actionPower[$roleVal['id']][$fatherData[$columnRolePower[0]]['model']][$actionArr[$columnRolePower[1]]] = true;
                    }
                }
                if($actionPower)  $this->handler->hset('actionPowerData', 'actionPower_'.$roleVal['id'], json_encode($actionPower[$roleVal['id']]));
                $this->setRoleColumnData($roleFatherData, $roleVal['id']);
            }else{
                //超级管理员
                $this->setRoleColumnData($fatherData, $roleVal['id']);
            }
        }
    }

    public function setRoleColumnData($fatherData, $roleId){
        $cloumnArr = array();

        foreach($fatherData as $key=>$val){
            switch($val['level']){
                case 1:
                        $cloumnArr[$key] = $val;
                    break;

                case 2:
                        $cloumnArr[$val['father_id']]['children'][$key] = $val;
                    break;

                case 3:
                    $cloumnArr[$fatherData[$val['father_id']]['father_id']]['children'][$val['father_id']]['children'][$key] = $val;
                    break;
            }
        }

        $this->handler->hset('columnRoleData', 'column_'.$roleId, json_encode($cloumnArr));
    }

}
